home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Editing / Greedy_Backspace.bsh < prev    next >
Encoding:
Text File  |  2004-08-29  |  1.9 KB  |  81 lines

  1. /*
  2.  * Greedy_Backspace.bsh - If buffer is using soft tabs,
  3.  * this macro will backspace to the previous tab stop,
  4.  * if all characters between the caret and the tab stop
  5.  * are spaces.  In all other cases a single character is
  6.  * removed.
  7.  *
  8.  * Copyright (C) 2002-2004 Ollie Rutherfurd <oliver@jedit.org>
  9.  *
  10.  * $Id: Greedy_Backspace.bsh,v 1.4 2004/06/03 22:09:22 orutherfurd Exp $
  11.  */
  12.  
  13. /**
  14.  * @param onlyFullTabs if true, multiple spaces are only
  15.  *                     removed if they would constitute
  16.  *                     a 'complete' tab.
  17.  */
  18. void greedyBackspace(View view, boolean onlyFullTabs)
  19. {
  20.     JEditTextArea textArea = view.getTextArea();
  21.     Buffer buffer = textArea.getBuffer();
  22.     int caret = textArea.getCaretPosition();
  23.     int caretLine = textArea.getCaretLine();
  24.     int lineStart = textArea.getLineStartOffset(caretLine);
  25.  
  26.     if(buffer.getBooleanProperty("noTabs") == true)
  27.     {
  28.         // if anything is selected, use standard 
  29.         if(textArea.getSelection().length != 0)
  30.         {
  31.             textArea.backspace();
  32.         }
  33.         // if at the start of the line, use standard
  34.         else if(caret == lineStart)
  35.         {
  36.             textArea.backspace();
  37.         }
  38.         else
  39.         {
  40.             int col = caret - lineStart;
  41.             int tabSize = buffer.getIntegerProperty("tabSize",8);
  42.  
  43.             // unlikely, but just in case
  44.             if(tabSize <= 0)
  45.             {
  46.                 buffer.remove(caret-1,1);
  47.             }
  48.             else
  49.             {
  50.                 int toTabStop = ((col-1) % tabSize) + 1;
  51.                 int count = 0;
  52.                 String chunk = buffer.getText(caret-toTabStop,toTabStop);
  53.                 for(int i=0; i < toTabStop; i++)
  54.                 {
  55.                     if(' ' != chunk.charAt(i))
  56.                         break;
  57.                     count += 1;
  58.                 }
  59.  
  60.                 // if onlyFullTabs must be only spaces to
  61.                 // the tabStop and must have tabSize number 
  62.                 // of spaces to remove them all.
  63.                 if(onlyFullTabs == false || count == tabSize){
  64.                     buffer.remove(caret-count,count);
  65.                 }
  66.                 else{
  67.                     buffer.remove(caret-1,1);
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     else
  73.         textArea.backspace();
  74. }
  75.  
  76. if(buffer.isReadOnly())
  77.     Toolkit.getDefaultToolkit().beep();
  78. else
  79.     greedyBackspace(view,true);
  80.  
  81.